package user

import (
	"context"
	"fmt"
	"git.mycaigou.com/gfyx/micro-gfyx-user-service/infrastructure/common/errcode"
	"git.mycaigou.com/gfyx/micro-gfyx-user-service/infrastructure/repository/remote/modeling_paltform_remote"
	pb "git.mycaigou.com/gfyx/micro-gfyx-user-service/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service"
	"git.mycaigou.com/gfyx/micro-gfyx-user-service/service/user"
	"git.myscrm.cn/golang/common/ykerrcode"
	"git.myscrm.cn/golang/stark/v4"
)

type UserInfo struct {
	UserInfoService user.UserInfoServiceIface
}

func NewUserInfo(userInfoService user.UserInfoServiceIface) pb.UserInfoServiceServer {
	return &UserInfo{
		UserInfoService: userInfoService,
	}
}

func (s *UserInfo) SyncTenantProfit(ctx context.Context, request *pb.SyncTenantProfitRequest) (*pb.SyncTenantProfitResponse, error) {
	/*var response = &pb.SyncTenantProfitResponse{}
	response.SyncFlag = false
	if request.TenantCode == "" {
		return nil, ykerrcode.TogRPCError(errcode.COMMON_PARAMS_ERROR, "租户code不能为空")
	}
	tenantProfitList := []*user2.GfyxModelSyncTenantProfit{}
	for _, v := range request.TenantProfitList {
		tenantProfitList = append(tenantProfitList, &user2.GfyxModelSyncTenantProfit{
			EffectiveEndTime:   v.EffectiveEndTime,
			EffectiveStartTime: v.EffectiveStartTime,
			ProductCode:        v.ProductCode,
			ProfitName:         v.ProfitName,
			ProfitOutsideCode:  v.ProfitOutsideCode,
			ProfitOutsideUID:   v.ProfitOutsideUid,
			UID:                v.Uid,
		})
	}
	flag, err := s.UserInfoService.SyncTenantProfit(ctx, request.TenantCode, tenantProfitList)
	if err != nil {
		stark.Logger.Error(ctx, err)
		return response, ykerrcode.TogRPCError(errcode.COMMON_ERROR, err.Error())
	}
	if flag == false {
		stark.Logger.Error(ctx, "租户权益同步失败，租户code："+request.TenantCode)
		return response, ykerrcode.TogRPCError(errcode.COMMON_ERROR, "租户权益同步失败，租户code："+request.TenantCode)
	}
	response.SyncFlag = true
	return response, nil*/
	return &pb.SyncTenantProfitResponse{}, nil
}

func (s *UserInfo) SyncAllUserRoleRelation(ctx context.Context, request *pb.SyncAllUserRoleRelationRequest) (*pb.SyncAllUserRoleRelationResponse, error) {
	/*var response = &pb.SyncAllUserRoleRelationResponse{}
	response.SyncFlag = false
	if request.TenantCode == "" {
		return nil, ykerrcode.TogRPCError(errcode.COMMON_PARAMS_ERROR, "租户code不能为空")
	}
	if request.Page < 0 {
		return nil, ykerrcode.TogRPCError(errcode.COMMON_PARAMS_ERROR, "页码必须大于等于0")
	}
	if request.Pagesize > 100 || request.Pagesize < 1 {
		return nil, ykerrcode.TogRPCError(errcode.COMMON_PARAMS_ERROR, "分页size必须大于等于1小于等于10")
	}

	flag, err := s.UserInfoService.SyncAllUserRoleRelation(ctx, request.TenantCode, request.Page, request.Pagesize)
	if err != nil {
		stark.Logger.Error(ctx, err)
		return response, ykerrcode.TogRPCError(errcode.COMMON_ERROR, err.Error())
	}
	if flag == false {
		stark.Logger.Error(ctx, "用户角色关系同步失败，租户code："+request.TenantCode)
		return response, ykerrcode.TogRPCError(errcode.COMMON_ERROR, "用户角色关系同步失败，租户code："+request.TenantCode)
	}

	response.SyncFlag = true
	return response, nil*/
	return &pb.SyncAllUserRoleRelationResponse{}, nil
}

// 同步全量用户权限
func (s *UserInfo) SyncAllUserActionRightInfos(ctx context.Context, request *pb.SyncAllUserActionRightInfosRequest) (*pb.SyncAllUserActionRightInfosResponse, error) {
	/*var response = &pb.SyncAllUserActionRightInfosResponse{}
	response.SyncFlag = false
	if request.TenantCode == "" {
		return nil, ykerrcode.TogRPCError(errcode.COMMON_PARAMS_ERROR, "租户code不能为空")
	}
	//if request.Page < 0 {
	//	return nil, ykerrcode.TogRPCError(errcode.COMMON_PARAMS_ERROR, "页码必须大于等于0")
	//}
	//if request.Pagesize > 10 || request.Pagesize < 1 {
	//	return nil, ykerrcode.TogRPCError(errcode.COMMON_PARAMS_ERROR, "分页size必须大于等于1小于等于10")
	//}

	flag, err := s.UserInfoService.SyncAllUserActionRightInfos(ctx, request.TenantCode)
	if err != nil {
		stark.Logger.Error(ctx, err)
		return response, ykerrcode.TogRPCError(errcode.COMMON_ERROR, err.Error())
	}
	if flag == false {
		stark.Logger.Error(ctx, "用户权限同步失败，租户code："+request.TenantCode)
		return response, ykerrcode.TogRPCError(errcode.COMMON_ERROR, "用户权限同步失败，租户code："+request.TenantCode)
	}

	response.SyncFlag = true
	return response, nil*/
	return &pb.SyncAllUserActionRightInfosResponse{}, nil
}

// 获取用户信息
func (s *UserInfo) GetUserInfo(ctx context.Context, request *pb.GetUserInfoRequest) (*pb.GetUserInfoResponse, error) {
	var response = &pb.GetUserInfoResponse{}
	commonUserInfoBo, err := s.UserInfoService.GetUserInfo(ctx, request.Type, request.Param)
	if err != nil {
		return response, err
	}
	userInfo := &pb.UserInfo{
		Id:          commonUserInfoBo.TenantUserId,
		UserName:    commonUserInfoBo.UserName,
		UserUid:     commonUserInfoBo.TenantUserUid,
		MpOpenId:    commonUserInfoBo.MpOpenid,
		Uid:         commonUserInfoBo.Uid,
		IsDisable:   commonUserInfoBo.IsDisable,
		UserCode:    commonUserInfoBo.UserCode,
		MobilePhone: commonUserInfoBo.MobilePhone,
		MpUnionId:   commonUserInfoBo.MpUnionId,
	}
	response.UserInfo = userInfo
	// 租户为空的是个人用户
	if commonUserInfoBo.TenantCode != "" {
		tenantProfitUse, err := s.UserInfoService.GetTenantProfitUse(ctx, commonUserInfoBo.TenantCode)
		if err != nil {
			return response, ykerrcode.TogRPCError(ykerrcode.FAIL, "获取租户权益失败")
		}
		tenantInfo := &pb.TenantInfo{
			TenantCode: commonUserInfoBo.TenantCode,
			TenantName: commonUserInfoBo.TenantName,
			// 租户线索雷达权益
			ClueRadarPayStatus: tenantProfitUse.ClueRadarPayStatus,
		}
		tenantInfoList := make([]*pb.TenantInfo, 0)
		tenantInfoList = append(tenantInfoList, tenantInfo)
		response.TenantInfo = tenantInfoList
	}
	return response, nil
}

func (s *UserInfo) SyncAllUser(ctx context.Context, request *pb.SyncUserRequest) (*pb.SyncUserResponse, error) {
	/*var response = &pb.SyncUserResponse{}
	response.SyncFlag = false
	if request.TenantCode == "" {
		return nil, ykerrcode.TogRPCError(errcode.COMMON_PARAMS_ERROR, "租户code不能为空")
	}
	if request.Page < 0 {
		return nil, ykerrcode.TogRPCError(errcode.COMMON_PARAMS_ERROR, "页码必须大于等于0")
	}
	if request.Pagesize > 10 || request.Pagesize < 1 {
		return nil, ykerrcode.TogRPCError(errcode.COMMON_PARAMS_ERROR, "分页size必须大于等于1小于等于10")
	}

	flag, err := s.UserInfoService.SyncAllUserInfo(ctx, request.TenantCode, request.Page, request.Pagesize)
	if err != nil {
		stark.Logger.Error(ctx, err)
		return response, ykerrcode.TogRPCError(errcode.COMMON_ERROR, err.Error())
	}
	if flag != false {
		stark.Logger.Error(ctx, "用户同步失败，租户code："+request.TenantCode)
		return response, ykerrcode.TogRPCError(errcode.COMMON_ERROR, "用户同步失败，租户code："+request.TenantCode)
	}

	response.SyncFlag = true
	return response, nil*/
	return &pb.SyncUserResponse{}, nil
}

func (s *UserInfo) IncrementSyncUser(ctx context.Context, request *pb.IncrementSyncUserRequest) (*pb.IncrementSyncUserResponse, error) {
	var response = &pb.IncrementSyncUserResponse{}
	response.SyncFlag = false
	if request.TenantCode == "" {
		return nil, ykerrcode.TogRPCError(errcode.COMMON_PARAMS_ERROR, "租户code不能为空")
	}
	fmt.Println(request.Operation)
	if request.Operation != "I" && request.Operation != "U" && request.Operation != "D" {
		return response, ykerrcode.TogRPCError(errcode.COMMON_PARAMS_ERROR, "操作类型错误")
	}
	if len(request.UserList) == 0 {
		return response, ykerrcode.TogRPCError(errcode.COMMON_PARAMS_ERROR, "用户列表不能为空")
	}

	var userList []modeling_platfrom_remote.UserInfo
	for _, info := range request.UserList {
		userInfo := modeling_platfrom_remote.UserInfo{}
		userInfo.UserId = info.UserId
		userInfo.UserGuid = info.UserUid
		userInfo.UserCode = info.UserCode
		userInfo.UserName = info.UserName
		userInfo.MobilePhone = info.MobilePhone
		userInfo.IsDisable = info.IsDisable
		userInfo.BuGuid = info.BuGuid
		userInfo.Password = info.Password
		userList = append(userList, userInfo)
	}
	flag, err := s.UserInfoService.IncrSyncUser(ctx, request.TenantCode, request.Operation, userList)
	if err != nil {
		stark.Logger.Error(ctx, err)
		return response, ykerrcode.TogRPCError(errcode.COMMON_ERROR, err.Error())
	}
	if flag != false {
		stark.Logger.Error(ctx, "用户同步失败，租户code："+request.TenantCode)
		return response, ykerrcode.TogRPCError(errcode.COMMON_ERROR, "用户同步失败，租户code："+request.TenantCode)
	}

	response.SyncFlag = true
	return response, nil
}
